RStudio session
In RStudio, a session begins when you open RStudio, and ends when you close the software. If you close the software, you’ll get a prompt asking you if you want to save your progress or not. However, you’re only able to save your R Scripts and R Data, but not all of your settings.
In this post, we’ll learn about all the necessary steps to follow each time you want to use R (or RStudio). But first, let’s recall how does RStudio look like when you open it.
Anatomy of RStudio
When you open RStudio, you may see a screen similar to this one:
There are at least 3 subscreens:
- Left: the Environment/History subscreen
- Upper Right hand side: The Console subscreen.
- Bottom Right hand side: The Files/Plots/Packages/Help/Viewer subscreen.
Perhaps these subscreens have a different arrangement in your computer. We’ll learn how to configure the position of these subscreens in a future post.
A very important subscreen that we need yet to see is the Scripts subscreen. In order to open it, you may do one of these:
-
Go to the File Menu > New File > R Script:
-
Click on the white sheet icon with a green plus sign located at the upper left corner of RStudio, and choose R Script:
-
Press
shift+Cmd+n
on Mac, orctrl+shift+n
on Mac and Windows.
Once you have opened your R Script subscreen, your RStudio session might look similar to this:
Getting started
The first step I recommend when using RStudio is to use R Scripts to write, edit and store all of your code. You may write anything you want in this script file, and RStudio will ignore it until you run that line of code.
The second step I always recommend to do, is to tell R and RStudio where to look for files in your own computer. This process is called “Setting the Working Directory”, and we can use the function setwd()
. In order to do this, you may want to use a similar line of code to this one when working from the computers in Kursraum 5:
setwd("/usr281/ben/msc15xx/rest_of_your_path")
Where:
-
setwd()
is the ‘set working directory’ function, -
msc15xx
is your personal msc number (writephd15xx
if you’re a PhD-CIH student, and replace the last two x’s with your own number), -
and
rest_of_your_path
refers to all the folders and subfolders that you have before getting to the one where your data are located at.
Note: The path should be enclosed in quotation marks to work!
If you’re working on a mac computer, your path might look something like this:
setwd("~/Documents/rest_of_your_folders")
Where ~
represents your User name (you don’t need to write your name, the ~
sign is enough), and Documents
could be some other folder (like your Desktop
, Dropbox
folder, etc.).
If you’re working on Windows, the path might look like this:
setwd("c:/docs/mydir")
And, as usual, be sure to change all of your folders according to your own computer’s architecture and configuration!!!.
Run commands
In order to run a command such as setwd()
, you can either go to the Run button located in the upper right corner of the R Script subscreen, or press cmd+Enter
on a mac (or ctrl+Enter
on Windows):
Every command you run from the R Script subscreen will appear in the Console subscreen, along with any output from R. If R doesn’t do anything (no errors, no warnings, no nothing), it means it worked but the command you used has no output.
Loading data
Once, you’ve set up your working directory, you can ask R to see which files are there in your working directory, just to make sure that your data files are there. If you run the dir()
function, you’re asking R to tell you which files are in your directory:
dir()
and it should return something like:
[1] "nhanesdataset_a.tsv" "nhanesdataset_b.tsv" "nhanesdataset_c.tsv"
[4] "nhanesdataset_d.tsv" "nhanesdataset_e.tsv"
You could have more files, but at least your data set files should be in there. If they are not, or if you get any errors, repeat setting the working directory and make sure you’re using the right path to the folder where your data set files are!
Then, you’re ready to read-in your data, using the read.table()
function. Use a code similar to the one we learned in the R course - Lecture 2:
tab <- read.table("nhanesdataset_a.tsv", sep="\t", header=TRUE)
To find out more about the read.table()
function, you could go to its help page by typing:
?read.table
Work on your data
Now you’re finally set to start analyzing your data!
Comments
I highly recommend using comments to specify what you’ve done, and either make it understandable for someone else, or for your future-self! In R, you can write the pound or hastag sign (#
), and everything you write after this sign will be a comment. Comments are ignored by R. The comment ends when you start writing in a new line:
In the image, lines 1, 4 and 7 are comments, and will be ignored by R. These comments help me remember that what I’ve written in lines 2, 5 and 8 is for setting the working directory, seeing the files withing my working directory, and reading the data, respectively.
Save your R Script:
Once you’re done working on RStudio, you should save your R Script to store all of the code that you used during this session. To save, just go to File > Save, and choose a name for your file:
Alternatively, you can also press cmd+s
on mac or ctrl+s
on windows.
Next time you open RStudio, you only need to run these lines of code again, without having to type your working directory path and everything.
Concluding remarks
A typical RStudio session workflow goes as follows:
- Open RStudio
- Open a new script file (or a previously saved script) and write everything in there
- Set your working directory
- Read-in your data
- Work on your data
- Use comments as often as you like
- Save your R Script